#!/usr/bin/env python
# -*- coding: utf-8 -*-
# author： 胡浩浩
# datetime： 2021/12/29 17:22 
# ide： PyCharm
from typing import List
from sqlalchemy.orm import Session
from fastapi import APIRouter, Depends, HTTPException
from Models.userModel import user_model
from Tools.database import engine
from Dependencies.db_depedencies import get_db
from Controllers.userController import user_controller
from Schemas.userSchema import user_schema

user_model.Base.metadata.create_all(bind=engine)
router = APIRouter()


@router.get("/user/get/list", response_model=List[user_schema.User])
async def get_user_list(skip: int = 0, limit: int = 10, db: Session = Depends(get_db)):
    user_list = user_controller.get_user_list(db=db, skip=skip, limit=limit)
    return user_list


@router.get("/user/get/{user_id}", response_model=user_schema.User)
async def get_user_by_id(user_id: int, db: Session = Depends(get_db)):
    user = user_controller.get_user_by_id(db=db, user_id=user_id)
    if user is None:
        raise HTTPException(status_code=404, detail="user not find")
    return user


@router.post("/user/create", response_model=user_schema.UserCreate)
async def create_user(user: user_schema.UserCreate, db: Session = Depends(get_db)):
    user = user_controller.create_user(db=db, user=user)
    return user


@router.get("/group/get/list", response_model=List[user_schema.Group])
async def get_group_list(skip: int = 0, limit: int = 10, db: Session = Depends(get_db)):
    group_list = user_controller.get_group_list(db=db, skip=skip, limit=limit)
    return group_list


@router.get("/group/get/{group_id}", response_model=user_schema.Group)
async def get_group_by_id(group_id: int, db: Session = Depends(get_db)):
    group = user_controller, get_group_by_id(db=db, group_id=group_id)
    if group is None:
        raise HTTPException(status_code=404, detail="group not find")
    return group


@router.get("/group/create", response_model=user_schema.Group)
async def create_group(group: user_schema.GroupCreate, db: Session = Depends(get_db)):
    group = user_controller.create_group(db=db, group=group)
    return group


@router.get("/user/group/relation/get/{user_id}", response_model=user_schema.UserGroupRelation)
async def get_user_group_relation_by_user_id(user_id: int, db: Session = Depends(get_db)):
    relation = user_controller.get_user_group_relation_by_user_id(db=db, user_id=user_id)
    return relation


@router.get("/user/group/relation/get/{group_id}", response_model=user_schema.UserGroupRelation)
async def get_user_group_relation_by_group_id(group_id: int, db: Session = Depends(get_db)):
    relation = user_controller.get_user_group_relation_by_group_id(db=db, group_id=group_id)
    return relation


@router.get("/user/group/relation/get/{relation_id}", response_model=user_schema.UserGroupRelation)
async def get_user_list(relation_id: int, db: Session = Depends(get_db)):
    relation = user_controller.get_user_group_relation_by_id(db=db, relation_id=relation_id)
    return relation


@router.post("/user/group/relation/create", response_model=user_schema.UserGroupRelation)
async def create_user_group_relation(relation: user_schema.UserGroupRelationCreate, db: Session = Depends(get_db)):
    relation = user_controller.create_user_group_relation(db=db, relation=relation)
    return relation



